package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;

import com.project.bean.AddressBean;

import com.project.bean.UserBean;
import com.project.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController {
    @Autowired
    private IAddressService addressService;
    
    @RequestMapping("findAll")
    public List<AddressBean> findByUserNum(HttpServletRequest request){
        UserBean user=(UserBean) request.getSession().getAttribute("loginUser");
        return addressService.findByUserNum(user.getUserNum());
    }
    
    /**
     * 添加新的收货地址
     * 需要判读新增地址状态是否为默认 是默认要把用户的原有默认地址改为普通地址
     * 如果不是，则直接添加
     * @param addressBean 地址对象
     */
    @RequestMapping("addAddress")
    public String addAddress(AddressBean addressBean,HttpServletRequest request){
        UserBean user= (UserBean) request.getSession().getAttribute("loginUser");
        addressBean.setUserNum(user.getUserNum());
        addressService.addAddress(addressBean);
        return "ok";
    }
    
    @RequestMapping("addList")
    public String addList(MultipartFile file, HttpServletRequest request) throws Exception {
        if(!file.isEmpty()){
            //设置初始化信息
            ImportParams params = new ImportParams();
            //设置选选项卡
            params.setSheetNum(1);
            //标题栏占一行
            //params.setTitleRows(1);
            //设置表头占用的行数
            params.setHeadRows(1);
            //读取上传文件的信息，封装成集合
            //上传文件的二进制流
            //实体类类模板
            //初始化信息
            List<AddressBean> list = ExcelImportUtil.importExcel(file.getInputStream(), AddressBean.class, params);
            UserBean user=(UserBean) request.getSession().getAttribute("loginUser");
            addressService.addList(list,user.getUserNum());
            return "ok";
        }
        return "on";
    }
    
    /**
     * 修改地址 需要判断修改的状态是不是默认地址
     * @param addressBean 收获地址对象
     */
    @RequestMapping("updateAddress")
    public String updateAddress(AddressBean addressBean,HttpServletRequest request){
        UserBean user=(UserBean) request.getSession().getAttribute("loginUser");
        addressBean.setUserNum(user.getUserNum());
        addressService.updateAddress(addressBean);
        return "ok";
    }
    
    /**
     * 修改默认地址 需要把原来的地址改成普通地址
     * @param addressId 设置默认地址的地址id
     */
    @RequestMapping("updateStatus")
    public String updateStatus(Long addressId){
        addressService.updateStatus(addressId);
        return "ok";
    }
    @RequestMapping("del")
    public String  del(Long addressId) {
        addressService.del(addressId);
        return "ok";
    }
    @RequestMapping("findById")
    public AddressBean findById(Long addressId) {
        return addressService.findById(addressId);
    }
}
